const express = require('express')
const app = express();           //新建App
var path=require('path');
const cors = require('cors');
const data_center_url="https://3c472fa2-ea4b-428a-bc28-52eceaed33a4.bspapp.com/http/"
var ManagerUser=require('./modules/ManagerUser.js')
let manager_user=new ManagerUser(data_center_url)
var Manage=require('./modules/Manage.js')
let manage=new Manage()
manage.openDatabase("local")
app.all('*', (req, res, next)=>{

res.header('Access-Control-Allow-Origin', '*');

//Access-Control-Allow-Headers ,可根据浏览器的F12查看,把对应的粘贴在这里就行

res.header('Access-Control-Allow-Headers', 'Content-Type');

res.header('Access-Control-Allow-Methods', '*');

// res.header('Content-Type', 'application/json;charset=utf-8');

next();

});
// app.use(cors({
// origin: 'http://localhost:3000',
// methods:['GET','POST'],
// alloweHeaders:['Conten-Type', 'Authorization'],
// credentials: true    // 是否带cookie
// }));
// app.use(cors({
// origin: 'http://localhost:4000',
// methods:['GET','POST'],
// alloweHeaders:['Conten-Type', 'Authorization'],
// credentials: true    // 是否带cookie
// }));
// POST数据的解析需要有中间件先解析一层
// body.urlencoded会给req加上一个body属性
// req.body本来就是不存在的
// 加上之后后面才可以正常输出
var bodyParser = require('body-parser');

app.use(bodyParser.json({limit: '5mb'}));    //最大上传大小不超过50mb

app.use(bodyParser.urlencoded({

 limit: '50mb',

 extended:true

}));

app.use(express.static(path.join(__dirname, 'dist')));




// app.get('/', function (req, res) { //请求，响应
//  console.log(req);
// res.send('<h1>服务器已经起动了，get请求</h1>')
// })
// app.get('/test1', function (req, res) { //请求，响应
//  console.log(req);
// res.send('<h1>测试1，get请求</h1>')
// })
// app.post('/', function (req, res) { //请求，响应
// // var data=JSON.parse(req.query[0])
// // console.log(data);
// res.send('<h1>服务器已经起动了，post请求</h1>')
// })
app.post('/login',async (req, res)=>{ //请求，响应
	let data=req.body
	let user_name=data.user_name
	let password=data.password
	let t=await manage.login(user_name,password);
	console.log(t)

	res.send(t);
	// try {
	// 	t=await manage.login();
	//    } catch(e) {
	// 	console.log('Error happend while connecting to the DB: ', e)
	//    }finally{
	// 	console.log(t)
	// 	let result={status:2}
	// 	res.send(result);
	//    }

})
app.post('/login_big_user',async (req, res)=>{ //请求，响应
	let data=req.body
	let user_name=data.user_name
	let password=data.password
	let t=await manage.loginBigUser(user_name,password);
	if(t.status!=2){
		console.log(t)
		res.send(t);
	}else{
		let user_id=t.result.link_department.department_id
		
		let user_info=await manage.getUserInfoById(user_id)
		console.log(user_info)
		let res2=t
		res2.user_info=user_info
		console.log(res2)
		res.send(res2);
	}

})
app.post('/add_excel',async (req, res)=>{ //请求，响应
	let data=req.body
	let author_id=data.author_id
	let author_name=data.author_name
	let excel_name=data.excel_name
	let date=data.date
	let content=data.content
	let result=await manage.addExcel(author_id,author_name,excel_name,content,date)
	let excel_id=result.result.insertedId
	let excel_list=await manage.getExcelListByAuthorId(author_id)
	console.log(author_name)
	let finall_res=await manage.addExcelSummaryToExcelList(author_id,excel_list,excel_id,excel_name,date,author_name)
	res.send(finall_res);
})
app.post('/add_user',async (req, res)=>{ //请求，响应
	let data=req.body
	let user=data.user
	let result=await manage.addUser(user)
	res.send(result);
})
app.post('/add_big_user',async (req, res)=>{ //请求，响应
	let data=req.body
	let user=data.user
	let result=await manage.addBigUser(user)
	res.send(result);
})
app.post('/update_link_superior',async (req, res)=>{ //请求，响应
	let data=req.body
	let old_superior_id=data.old_superior_id
	let user_id=data.user_id
	let superior_id=data.superior_id
	let superior_name=data.superior_name
	let department_name=data.department_name
	let update_user_link=await manage.userLink(user_id,superior_id,superior_name)
	if(old_superior_id!=false){
		let old_list=await manage.getOldSuperiorSubDepartment(old_superior_id)
		let update_old_superior=await manage.updateOldSuperior(old_list,old_superior_id,user_id)
	}
	let new_list=await manage.getNewSuperiorSubDepartment(superior_id)
	let update_new_superior=await manage.updateNewSuperior(new_list,superior_id,user_id, department_name)
	res.send({"status":2});
})
app.post('/update_link_department',async (req, res)=>{ //请求，响应
	let data=req.body
	let user_id=data.user_id
	let link_info=data.link_info
	let update_user_link=await manage.bigUserLink(user_id,link_info)
	res.send({"status":2});
})
app.post('/upload_pdf',async (req, res)=>{ //请求，响应
	let data=req.body
	let pdf_name=data.pdf_name
	let user_id=data.user_id
	let user_name=data.user_name
	let pdfbase64=data.pdfbase64
	let date=data.date
	let result=await manage.addPdf(pdf_name,user_id,user_name,pdfbase64,date)
	res.send(result);
})
app.post('/get_excel_list_by_user_id',async (req, res)=>{ //请求，响应
	let data=req.body
	let user_id=data.user_id
	let result=await manage.getExcelListByAuthorId(user_id)
	res.send(result);
})
app.post('/get_pdf_by_user_id',async (req, res)=>{ //请求，响应
	let data=req.body
	let user_id=data.user_id
	let result=await manage.getPdfByUserId(user_id)
	res.send(result);
})
app.post('/update_excel',async (req, res)=>{ //请求，响应
	let data=req.body
	let excel_id=data.excel_id
	let content=data.content
	let author_id=data.author_id
	let excel_name=data.excel_name
	let excel_list=await manage.getExcelListByAuthorId(author_id)
	let result=await manage.updateExcel(excel_id,content,excel_name)
	let finall_res=await manage.updateExcelSummaryToExcelList(author_id,excel_id,excel_name,excel_list)
	res.send(finall_res);
})
app.post('/update_password',async (req, res)=>{ //请求，响应
	let data=req.body
	let user_id=data.user_id
	let password=data.password
	let result=await manage.updatePassword(user_id,password)
	res.send(result);
})
app.post('/update_big_user_password',async (req, res)=>{ //请求，响应
	let data=req.body
	let user_id=data.user_id
	let password=data.password
	let result=await manage.updateBigPassword(user_id,password)
	res.send(result);
})
//根据user_id判断是否是作者从而判断是否只读
app.post('/get_excel__by_excel_id',async (req, res)=>{ //请求，响应
	let data=req.body
	let excel_id=data.excel_id
	let result=await manage.getExcelByExcelId(excel_id)
	res.send(result)
})
app.post('/get_users',async (req, res)=>{ //请求，响应
	let result=await manage.getUsers()
	res.send(result)
})
app.post('/get_big_users',async (req, res)=>{ //请求，响应
	let result=await manage.getBigUsers()
	res.send(result)
})
app.post('/delete_excel__by_excel_id',async (req, res)=>{ //请求，响应
	let data=req.body
	let excel_id=data.excel_id
	let user_id=data.user_id
	let excel_list=await manage.getExcelListByAuthorId(user_id)
	let res1=await  manage.deleteExcelFromUserList(excel_id,user_id,excel_list)
	let result=await manage.deleteExcelByExcelId(excel_id)
	res.send({"status":2})
})

app.post('/delete_pdf__by_id',async (req, res)=>{ //请求，响应
	let data=req.body
	let _id=data.pdf_id
	let result=await manage.deletePdfByPdfId(_id)
	res.send({"status":2})
})
app.post('/delete_user__by_id',async (req, res)=>{ //请求，响应
	let data=req.body
	let _id=data.user_id
	let result=await manage.deleteUserById(_id)
	res.send({"status":2})
})
app.post('/delete_big_user_user__by_id',async (req, res)=>{ //请求，响应
	let data=req.body
	let _id=data.user_id
	let result=await manage.deleteBigUserById(_id)
	res.send({"status":2})
})
app.get('/test1', function (req, res) { //请求，响应
 console.log(req);
res.send('<h1>测试1，get请求</h1>')
})

app.listen(8888, function () {  //监听9093端口
console.log('node app start at port 8888')
})